from utils import platform
from lib.net.index import wlan


class HTTP:
    def __init__(self):
        wlan.connect()

    def GET(self, url, params=None, type = "json"):
        if params is None:
            params = {}
        if platform.is_esp32_series():
            return self.ESP32_GET(url, params, type)
        if platform.is_raspberry_pico_series():
            return self.AT_GET(url, params, type)

    def POST(self, url, payload=None, type ="json"):
        if payload is None:
            payload = {}
        if platform.is_esp32_series():
            return self.ESP32_POST(url, payload, type)
        if platform.is_raspberry_pico_series():
            return self.AT_POST(url, payload, type)

    def AT_GET(self, url, params=None, type ="json"):
        if params is None:
            params = {}
        from lib.net.http import http_at

        res = http_at.GET(url, params, type)
        wlan.disconnect()
        return res

    def AT_POST(self, url, payload=None, type ="json"):
        if payload is None:
            payload = {}
        from lib.net.http import http_at
        res = http_at.POST(url, payload, type)
        wlan.disconnect()
        return res

    def ESP32_GET(self, url, params=None, type ="json"):
        if params is None:
            params = {}
        from lib.net.http import http_esp32
        return http_esp32.GET(url, params, type)

    def ESP32_POST(self, url, payload=None, type ="json"):
        if payload is None:
            payload = {}
        from lib.net.http import http_esp32

        return http_esp32.POST(url, payload, type)


http = HTTP()

if __name__ == "__main__":
    res = http.GET("http://www.hanxiaoxin.cn")
    print(res)
    res = http.GET("http://www.hanxiaoxin.cn/demo/index.html")
    print(res)